package com.example.mediamoudle.utlis

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class PermissionUtils {
    companion object{
         //是否已经有权限
         @JvmStatic
        fun hasPermissionRequest(context: Context, vararg permissions :String):Boolean{
            for (permission in permissions){
                if (!isPermissionRequest(context,permission)){
                    return false
                }
            }
            return true
        }


        /**
         * 是否授权
         */
        @JvmStatic
        fun isPermissionRequest(context: Context, permission: String): Boolean {
            return try {
                val selfPermission = ContextCompat.checkSelfPermission(context, permission)
                selfPermission == PackageManager.PERMISSION_GRANTED
            }catch (e:Exception){
                false
            }

        }

        /**
         * 权限是否被拒绝
         */
        @JvmStatic
        fun shouldShowRequestPermissionRationale(activity: Activity, vararg permissions: String): Boolean {
            for (permission in permissions) {
                if (activity?.let { ActivityCompat.shouldShowRequestPermissionRationale(it, permission) } == true) {
                    return true
                }
            }
            return false
        }

        /**
         * 权限是否请求成功
         */
        @JvmStatic
        fun requestPermissionSuccess(vararg gantedResult: Int): Boolean {
            if (gantedResult == null || gantedResult.isEmpty()) {
                return false
            }
            for (permissionValue in gantedResult) {
                if (permissionValue != PackageManager.PERMISSION_GRANTED) {
                    return false
                }
            }
            return true
        }
    }


}